package com.dianping.cat.home.service.transform;

import com.dianping.cat.home.service.IEntity;
import com.dianping.cat.home.service.entity.Domain;
import com.dianping.cat.home.service.entity.ServiceReport;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;




public class DefaultJsonParser<T>
{
  private DefaultLinker m_linker;
  private Stack<String> m_tags;
  private Stack<Object> m_objs;
  private List<T> m_entities;
  private Class<?> m_entityClass;

  private DefaultJsonParser(Class<?> entityClass) {
    this.m_linker = new DefaultLinker(true);

/*  33 */     this.m_tags = new Stack();

/*  35 */     this.m_objs = new Stack();

/*  37 */     this.m_entities = new ArrayList();




/*  42 */     this.m_entityClass = entityClass;
  }


/*  46 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, InputStream in) throws IOException { return (T)parse(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> T parse(Class<T> entityClass, Reader reader) throws IOException {
/*  51 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  53 */     parser.onArrayBegin();
/*  54 */     parser.parse(new JsonReader(reader));
/*  55 */     parser.onArrayEnd();

/*  57 */     if (parser.m_entities.isEmpty()) {
/*  58 */       return null;
    }
/*  60 */     return (T)(IEntity)parser.m_entities.get(0);
  }



/*  65 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, String json) throws IOException { return (T)parse(entityClass, new StringReader(json)); }



/*  69 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, InputStream in) throws Exception { return parseArray(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, Reader reader) throws Exception {
/*  74 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  76 */     parser.parse(new JsonReader(reader));
/*  77 */     return (List<T>) parser.m_entities;
  }


/*  81 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, String json) throws Exception { return parseArray(entityClass, new StringReader(json)); }




  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  87 */     if (value == null || value.length() == 0) {
/*  88 */       return defaultValue;
    }

/*  91 */     if (type == Boolean.class)
/*  92 */       return (T)Boolean.valueOf(value); 
/*  93 */     if (type == Integer.class)
/*  94 */       return (T)Integer.valueOf(value); 
    if (type == Long.class)
/*  96 */       return (T)Long.valueOf(value); 
/*  97 */     if (type == Short.class)
/*  98 */       return (T)Short.valueOf(value); 
/*  99 */     if (type == Float.class)
/* 100 */       return (T)Float.valueOf(value); 
/* 101 */     if (type == Double.class)
/* 102 */       return (T)Double.valueOf(value); 
/* 103 */     if (type == Byte.class)
/* 104 */       return (T)Byte.valueOf(value); 
/* 105 */     if (type == Character.class) {
/* 106 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 108 */     return (T)value;
  }


  private Object createRootEntity() {
    try {
/* 114 */       return this.m_entityClass.newInstance();

    }
/* 117 */     catch (Exception e) {
/* 118 */       throw new RuntimeException(String.format("Unable to create entity(%s) instance!", new Object[] { this.m_entityClass.getName() }), e);
    }
  }


/* 123 */   private boolean isTopLevel() { return (this.m_objs.size() == 1); }


  protected void onArrayBegin() {
/* 127 */     if (this.m_objs.isEmpty()) {
/* 128 */       this.m_objs.push(this.m_entities);
/* 129 */       this.m_tags.push("");
    }
  }
  protected void onArrayEnd() {
/* 133 */     this.m_objs.pop();
/* 134 */     this.m_tags.pop();
  }


/* 138 */   protected void onName(String name) { this.m_tags.push(name); }


  protected void onObjectBegin() {
/* 142 */     if (isTopLevel()) {
/* 143 */       this.m_objs.push(createRootEntity());
/* 144 */       this.m_tags.push("");
    } else {
/* 146 */       Object parent = this.m_objs.peek();
/* 147 */       String tag = (String)this.m_tags.peek();

/* 149 */       if (parent instanceof ServiceReport) {
/* 150 */         if ("domains".equals(tag)) {
/* 151 */           this.m_objs.push(parent);
        } else {
/* 153 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 155 */           if ("domains".equals(parentTag)) {
/* 156 */             Domain domains = new Domain();

/* 158 */             this.m_linker.onDomain((ServiceReport)parent, domains);
/* 159 */             this.m_objs.push(domains);
          } else {
/* 161 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
      } else {
/* 165 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }

  protected void onObjectEnd() {
/* 171 */     this.m_tags.pop();

/* 173 */     Object entity = this.m_objs.pop();

/* 175 */     if (isTopLevel()) {
/* 176 */       this.m_entities.add((T)entity);
    }
  }

  protected void onValue(String value) {
/* 181 */     Object parent = this.m_objs.peek();
/* 182 */     String tag = (String)this.m_tags.pop();

/* 184 */     if (parent instanceof ServiceReport) {
/* 185 */       parseForServiceReport((ServiceReport)parent, tag, value);
/* 186 */     } else if (parent instanceof Domain) {
/* 187 */       parseForDomain((Domain)parent, tag, value);
    } else {
/* 189 */       throw new RuntimeException(String.format("Unknown tag(%s) under %s!", new Object[] { tag, parent }));
    }
  }

  private void parse(JsonReader reader) throws IOException {
    try {
/* 195 */       reader.parse(this);
/* 196 */     } catch (EOFException e) {
/* 197 */       if (this.m_objs.size() > 1) {
/* 198 */         throw new EOFException(String.format("Unexpected end while parsing json! tags: %s.", new Object[] { this.m_tags }));
      }
    }

/* 202 */     this.m_linker.finish();
  }

  public void parseForDomain(Domain domain, String tag, String value) {
/* 206 */     if ("id".equals(tag)) {
/* 207 */       domain.setId(value);
/* 208 */     } else if ("totalCount".equals(tag)) {
/* 209 */       domain.setTotalCount(((Long)convert(Long.class, value, Long.valueOf(0L))).longValue());
/* 210 */     } else if ("failureCount".equals(tag)) {
/* 211 */       domain.setFailureCount(((Long)convert(Long.class, value, Long.valueOf(0L))).longValue());
/* 212 */     } else if ("failurePercent".equals(tag)) {
/* 213 */       domain.setFailurePercent(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 214 */     } else if ("sum".equals(tag)) {
/* 215 */       domain.setSum(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 216 */     } else if ("avg".equals(tag)) {
/* 217 */       domain.setAvg(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 218 */     } else if ("qps".equals(tag)) {
/* 219 */       domain.setQps(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
    } else {
/* 221 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, domain, this.m_tags }));
    }
  }

  public void parseForServiceReport(ServiceReport serviceReport, String tag, String value) {
/* 226 */     if (!"domains".equals(tag))
    {
/* 228 */       if ("startTime".equals(tag)) {
/* 229 */         serviceReport.setStartTime(toDate(value, "yyyy-MM-dd HH:mm:ss"));
/* 230 */       } else if ("domain".equals(tag)) {
/* 231 */         serviceReport.setDomain(value);
/* 232 */       } else if ("endTime".equals(tag)) {
/* 233 */         serviceReport.setEndTime(toDate(value, "yyyy-MM-dd HH:mm:ss"));
      } else {
/* 235 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, serviceReport, this.m_tags }));
      }
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 242 */       return (new SimpleDateFormat(format)).parse(str);
/* 243 */     } catch (ParseException e) {
/* 244 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
  static class JsonReader { private Reader m_reader;
    private char[] m_buffer;

    public JsonReader(Reader reader) {
/* 251 */       this.m_buffer = new char[2048];






/* 258 */       this.m_reader = reader;
    }
    private int m_size; private int m_index;
    private char next() throws IOException {
/* 262 */       if (this.m_index >= this.m_size) {
/* 263 */         this.m_size = this.m_reader.read(this.m_buffer);
/* 264 */         this.m_index = 0;

/* 266 */         if (this.m_size == -1) {
/* 267 */           throw new EOFException();
        }
      }

/* 271 */       return this.m_buffer[this.m_index++];
    }

    public void parse(DefaultJsonParser parser) throws IOException {
/* 275 */       StringBuilder sb = new StringBuilder();
/* 276 */       boolean flag = false;

      while (true) {
/* 279 */         char ch = next();

/* 281 */         switch (ch) {
          case '\t':
          case '\n':
          case '\r':
          case ' ':
            continue;
          case '{':
/* 288 */             parser.onObjectBegin();
/* 289 */             flag = false;
            continue;
          case '}':
/* 292 */             if (flag) {
/* 293 */               parser.onValue(sb.toString());
/* 294 */               sb.setLength(0);
            }

/* 297 */             parser.onObjectEnd();
/* 298 */             flag = false;
            continue;
          case '"':
          case '\'':
            while (true) {
/* 303 */               char ch2 = next();

/* 305 */               if (ch2 != ch) {
/* 306 */                 if (ch2 == '\\') {
/* 307 */                   char ch3 = next();

/* 309 */                   switch (ch3) {
                    case 't':
/* 311 */                       sb.append('\t');
                      continue;
                    case 'r':
/* 314 */                       sb.append('\r');
                      continue;
                    case 'n':
/* 317 */                       sb.append('\n');
                      continue;
                  }
/* 320 */                   sb.append(ch3);

                  continue;
                }
/* 324 */                 sb.append(ch2); continue;
              }  break;
            }
/* 327 */             if (!flag) {
/* 328 */               parser.onName(sb.toString());
            } else {
/* 330 */               parser.onValue(sb.toString());
/* 331 */               flag = false;
            }

/* 334 */             sb.setLength(0);
            continue;




          case ':':
/* 341 */             if (sb.length() != 0) {
/* 342 */               parser.onName(sb.toString());
/* 343 */               sb.setLength(0);
            }

/* 346 */             flag = true;
            continue;
          case ',':
/* 349 */             if (sb.length() != 0) {
/* 350 */               if (!flag) {
/* 351 */                 parser.onName(sb.toString());
              } else {
/* 353 */                 parser.onValue(sb.toString());
              }

/* 356 */               sb.setLength(0);
            }

/* 359 */             flag = false;
            continue;
          case '[':
/* 362 */             parser.onArrayBegin();
/* 363 */             flag = false;
            continue;
          case ']':
/* 366 */             parser.onArrayEnd();
/* 367 */             flag = false;
            continue;
        }
/* 370 */         sb.append(ch);
      }
    } }

}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/transform/DefaultJsonParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */